package com.xnyzc.lhy.order.service.impl.vehicle;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.constant.MessageConstant;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.order.entity.order.OaCOrder;
import com.xnyzc.lhy.order.entity.common.bill.BillRulesCommon;
import com.xnyzc.lhy.order.entity.result.vehicle.DriverVehInfo;
import com.xnyzc.lhy.order.entity.vehicle.OaDDriverVehicle;
import com.xnyzc.lhy.order.entity.vehicle.OaSysVehicle;
import com.xnyzc.lhy.order.entity.vehicle.OaSysVehicleMdict;
import com.xnyzc.lhy.order.mapper.vehicle.OaDDriverVehicleMapper;
import com.xnyzc.lhy.order.mapper.vehicle.OaSysVehicleMapper;
import com.xnyzc.lhy.order.mapper.vehicle.OaSysVehicleMdictMapper;
import com.xnyzc.lhy.order.service.vehicle.IOaDDriverVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author: huxuekuo
 * @Date: 2019-08-05 17:19
 * @Description:
 */
@Service
public class OaDDriverVehicleServiceImpl extends ServiceImpl<OaDDriverVehicleMapper, OaDDriverVehicle> implements IOaDDriverVehicleService {


    @Autowired
    private OaDDriverVehicleMapper dDriverVehicleMapper;

    @Autowired
    private OaSysVehicleMapper sysVehicleMapper;

    @Autowired
    private BillRulesCommon billRulesCommon;

    @Autowired
    private OaSysVehicleMdictMapper vehicleMdictMapper;

    /**
     * netty 获取司机车辆信息
     *
     * @return
     */
    @Override
    public DriverVehInfo getDDriverVehicleInfo(String vehicleId) {
        DriverVehInfo driverVehInfo = new DriverVehInfo();
        OaDDriverVehicle oaDDriverVehicle = dDriverVehicleMapper.selectOne(Qw.create().eq(OaDDriverVehicle.DRIVER_ID, vehicleId));
        if (CheckUtil.objIsEmpty(oaDDriverVehicle)) {
            return null;
        }
        driverVehInfo.setDriverId(oaDDriverVehicle.getDriverId());
        driverVehInfo.setVehId(oaDDriverVehicle.getVehicleId());
        //获取订单状态
        Long vehicleId1 = oaDDriverVehicle.getVehicleId();
        OaSysVehicle oaSysVehicle = sysVehicleMapper.selectById(vehicleId1);
        if (CheckUtil.objIsEmpty(oaSysVehicle)) {
            return null;
        }
        //获取车辆类型
        try {
            OaSysVehicleMdict oaSysVehicleMdict = vehicleMdictMapper.selectOne(Qw.create().eq(OaSysVehicleMdict.CODE, oaSysVehicle.getCode()).eq(OaSysVehicleMdict.EXPAND1, 3));
            if (CheckUtil.objIsEmpty(oaSysVehicleMdict)) {
                throw PangException.create(EErrorCode.argCheckErr, MessageConstant.DUSER_VEHICLE_ERROR);
            }
            driverVehInfo.setVehType(oaSysVehicleMdict.getVehicleType());
            return driverVehInfo;
        } catch (Exception e) {
            throw PangException.create(EErrorCode.argCheckErr, MessageConstant.DUSER_VEHICLE_ERROR);
        }
    }


    /**
     * netty 获取车辆信息
     *
     * @return
     */
    @Override
    public OaSysVehicle getSysVehicleInfo(Long vehicleId) {
        OaSysVehicle oaSysVehicle = sysVehicleMapper.selectById(vehicleId);
        return oaSysVehicle;
    }

    /**
     * netty 获取里程费
     *
     * @return
     */
    @Override
    public BigDecimal getMileageFee(String rulesId, Date date, BigDecimal subtract, BigDecimal totailMileage, String tag) {
        BigDecimal mileageFee = billRulesCommon.mileageFee(rulesId, date, subtract, totailMileage, tag);
        return mileageFee;
    }

    /**
     * netty 获取时长费
     *
     * @return
     */
    @Override
    public BigDecimal getTimeFee(String rulesId, BigDecimal currentTime, BigDecimal totalTime, String tag, Date startDate) {
        BigDecimal timeFee = billRulesCommon.timeFee(rulesId, currentTime, totalTime, tag, startDate);
        return timeFee;
    }

    /**
     * 获取时长费
     *
     * @param oaCOrder
     * @return
     */
    @Override
    public OaCOrder getTimeFee(OaCOrder oaCOrder) {
        return billRulesCommon.timeFee(oaCOrder);
    }

    @Override
    public OaCOrder getMileageFee(OaCOrder oaCOrder) {
        return billRulesCommon.getMileageFee(oaCOrder);
    }

    @Override
    public OaCOrder getDistanceFee(OaCOrder oaCOrder) {
        return billRulesCommon.getDistanceFee(oaCOrder);
    }

    /**
     * netty 获取远途费
     *
     * @param rulesId       计费规则ID
     * @param date          当前时间
     * @param subtract      当前行驶里程
     * @param totailMileage 总里程
     * @param tag           标签
     * @return
     */
    @Override
    public BigDecimal getDistanceFee(String rulesId, Date date, BigDecimal subtract, BigDecimal totailMileage, String tag) {
        BigDecimal distanceFee = billRulesCommon.getDistanceFee(totailMileage, subtract, date, rulesId, tag);
        return distanceFee;
    }


    @Override
    public BigDecimal getDrawFee(String rulesId, BigDecimal totalBillFee) {
        BigDecimal drawFee = billRulesCommon.getDrawFee(totalBillFee, rulesId);
        return drawFee;
    }


}
